//
//  EverydayViewController.m
//  B段_开眼
//
//  Created by hu on 16/4/19.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "EverydayViewController.h"

@interface EverydayViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *array;

@property (nonatomic, strong) NSString *nextPage;

@property (nonatomic, strong) UITableView *tableV;
// 刷新状态
@property (nonatomic, assign) BOOL isUpState;

@end

@implementation EverydayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
   
    _tableV.delegate = self;
    _tableV.dataSource = self;
    _tableV.backgroundColor = [UIColor cyanColor];
    _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    [_tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-49);
    }];
    // 添加下拉刷新控件 Block在下拉刷新开始时调用
    _tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isUpState = YES;
        
        self.nextPage = @"http://baobab.wandoujia.com/api/v2/feed?&num=7";
        [self getData];
    }];
    // 添加上拉加载控件 Block在上拉加载开始时调用
    _tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _isUpState = NO;
        [self getData];
    }];
    
    self.array = [NSMutableArray array];
    self.nextPage = @"http://baobab.wandoujia.com/api/v2/feed?&num=7";
    [self getData];
    
}

- (void)getData
{
    [EveryViewModel getDataWithPage:self.nextPage andUpdate:self.isUpState andBlock:^(NSMutableArray *arr, NSString *str) {
        if (self.isUpState == YES) {
            [self.array removeAllObjects];

        }
        [self.array addObjectsFromArray:arr];
        self.nextPage = str;
        NSLog(@"%@", _array);
        [_tableV.mj_header endRefreshing];
        [_tableV.mj_footer endRefreshing];
        [_tableV reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[_array objectAtIndex:section] objectForKey:@"array"] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCellFactory *cell = [TableViewCellFactory cellWithTableView:tableView identifier:@"everyVCTabelViewCell" model:[[[self.array objectAtIndex:indexPath.section] objectForKey:@"array"] objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    if ([[[_array objectAtIndex:section] objectForKey:@"type"] isEqualToString:@"textHeader"]) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x - 50, 10, 100, 30)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [[_array objectAtIndex:section] objectForKey:@"title"];
        [view addSubview:label];
    } else if ([[[_array objectAtIndex:section] objectForKey:@"type"] isEqualToString:@"imageHeader"]) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x - 50, 0, 100, 50)];
        [imageV sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:section] objectForKey:@"title"]]];
        [view addSubview:imageV];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([[[self.array objectAtIndex:section] objectForKey:@"type"] isEqualToString:@"banner1"]) {
        return 0.1;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
